<template>
  <div class="logs-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="search-form">
      <el-form-item label="日期范围" prop="dateRange">
        <el-date-picker
        v-model="queryParams.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="YYYY-MM-DD" 
        style="width: 300px;"
        />
      </el-form-item>
      <el-form-item label="登录用户" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入登录用户名"
          clearable
          style="width: 200px;"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="logsList" border stripe v-loading="loading">
      <el-table-column prop="username" label="登录用户名" width="120" />
      <el-table-column prop="logContent" label="操作内容" width="150" />
      <el-table-column prop="ipAddress" label="IP地址" width="150" />
      <el-table-column prop="os" label="操作系统" width="150" />
      <el-table-column prop="ie" label="浏览器" width="150" />
      <el-table-column prop="createDate" label="登录时间" width="180" :formatter="formatDate" />
    </el-table> 
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from '@/utils/axios'; // 根据你的axios封装调整

const queryParams = ref({
  dateRange: [],
  username: '',
  pageNum: 1,
  pageSize: 10
});

const total = ref(0);
const logsList = ref([]);
const loading = ref(false);

// 获取日志数据
const getList = () => {
  loading.value = true;
  
  // 构造符合后端要求的参数
  const params = {
    username: queryParams.value.username,
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  
  // 添加日期参数（使用后端需要的参数名）
  if (queryParams.value.dateRange && queryParams.value.dateRange.length === 2) {
    params.startDate = queryParams.value.dateRange[0];
    params.endDate = queryParams.value.dateRange[1];
  }
  
  axios.get('/api/system/logs', { params }).then(res => {
    // 修改1: 直接使用 res.data 而不是 res.data.list
    logsList.value = res.data;
    
    // 修改2: 从响应头获取总条数
    total.value = parseInt(res.headers['x-total-count'] || 0);
    
  }).catch(error => {
    console.error('获取日志失败:', error);
    logsList.value = [];
    total.value = 0;
  }).finally(() => {
    loading.value = false;
  });
};
function formatDate(row, column, cellValue) {
  if (!cellValue) return '';
  const date = new Date(cellValue);
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  const h = String(date.getHours()).padStart(2, '0');
  const min = String(date.getMinutes()).padStart(2, '0');
  const s = String(date.getSeconds()).padStart(2, '0');
  return `${y}-${m}-${d} ${h}:${min}:${s}`;
};

// 处理查询
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 重置查询
const resetQuery = () => {
  queryParams.value = {
    dateRange: [],
    username: '',
    pageNum: 1,
    pageSize: 10
  };
  getList();
};

// 处理分页变化
const handlePageChange = (page) => {
  queryParams.value.pageNum = page;
  getList();
};

// 处理每页数量变化
const handleSizeChange = (size) => {
  queryParams.value.pageSize = size;
  queryParams.value.pageNum = 1;
  getList();
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.logs-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>